package com.ziq.zcamp.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

public class FlowLayout extends ViewGroup {
    private int mVerticalSpacing; //每个item纵向间距
    private int mHorizontalSpacing; //每个item横向间距

    private int mMaxLineCount;
    private RemoveChildViewListener mRemoveChildListener;


    public FlowLayout(Context context) {
        super(context);
    }

    public FlowLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setHorizontalSpacing(int pixelSize) {
        mHorizontalSpacing = pixelSize;
    }

    public void setVerticalSpacing(int pixelSize) {
        mVerticalSpacing = pixelSize;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int selfWidth = resolveSize(0, widthMeasureSpec);

        int paddingLeft = getPaddingLeft();
        int paddingTop = getPaddingTop();
        int paddingRight = getPaddingRight();
        int paddingBottom = getPaddingBottom();

        int childLeft = paddingLeft;
        int childTop = paddingTop;
        int lineHeight = 0;

        int lineCount = 0;
        if(getChildCount()!=0){
            lineCount = 1;
        }

        //通过计算每一个子控件的高度，得到自己的高度
        for (int i = 0, childCount = getChildCount(); i < childCount; ++i) {
            View childView = getChildAt(i);
            if(childView.getVisibility() == View.GONE){
                continue;
            }
            LayoutParams childLayoutParams = childView.getLayoutParams();
            childView.measure(getChildMeasureSpec(widthMeasureSpec, paddingLeft + paddingRight,
                    childLayoutParams.width), getChildMeasureSpec(heightMeasureSpec,
                    paddingTop + paddingBottom, childLayoutParams.height));
            int childWidth = childView.getMeasuredWidth();
            int childHeight = childView.getMeasuredHeight();

            if(childLeft + childWidth + paddingRight > selfWidth && mMaxLineCount != 0){
                lineCount+=1;
                if(lineCount>mMaxLineCount){
                    if(mRemoveChildListener!=null){
                        mRemoveChildListener.removeChildView(i);
                    }
                    removeViews(i,childCount-i);
                    break;
                }
            }

            lineHeight = Math.max(childHeight, lineHeight);

            if (childLeft + childWidth + paddingRight > selfWidth) {
                childLeft = paddingLeft;
                childTop += mVerticalSpacing + lineHeight;
                lineHeight = childHeight;
            } else {

            }
            childLeft += childWidth + mHorizontalSpacing;
        }

        int wantedHeight = childTop + lineHeight + paddingBottom;
        setMeasuredDimension(selfWidth, resolveSize(wantedHeight, heightMeasureSpec));
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int myWidth = r - l;

        int paddingLeft = getPaddingLeft();
        int paddingTop = getPaddingTop();
        int paddingRight = getPaddingRight();

        int childLeft = paddingLeft;
        int childTop = paddingTop;

        int lineHeight = 0;

        //根据子控件的宽高，计算子控件应该出现的位置。
        for (int i = 0, childCount = getChildCount(); i < childCount; ++i) {
            View childView = getChildAt(i);

            if (childView.getVisibility() == View.GONE) {
                continue;
            }

            int childWidth = childView.getMeasuredWidth();
            int childHeight = childView.getMeasuredHeight();

            lineHeight = Math.max(childHeight, lineHeight);

            if (childLeft + childWidth + paddingRight > myWidth) {
                childLeft = paddingLeft;
                childTop += mVerticalSpacing + lineHeight;
                lineHeight = childHeight;
            }
            childView.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
            childLeft += childWidth + mHorizontalSpacing;
        }
    }

    /**
     * 维持行数不超过特定值, 最好设置删除子View的监听, 以同步数据容器的个数
     * @param lineCount [<= 0 代表无限制] [> 0 设置最大行数，超出最大行数则删掉多余的View并调用监听]
     */
    public void setMaxLine(int lineCount, RemoveChildViewListener listener){
        if(lineCount <= 0){
            mMaxLineCount = 0;
            mRemoveChildListener = null;
        }else{
            mMaxLineCount = lineCount;
            mRemoveChildListener = listener;
        }
        if(!isLayoutRequested()){
            requestLayout();
        }
    }

    public interface RemoveChildViewListener{
        /**
         * 当FlowLayout在布局时发现即将超出最大行数时，将会删除位于尾部的子View, 此时会回调该接口
         * !!!注意!!!不能执行耗时操作
         * @param sincePosition 从sincePosition开始删除的尾部子View
         */
        void removeChildView(int sincePosition);
    }
}